package com.ruoyi.control.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; //添加
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.control.domain.Mainventilator1History;
import com.ruoyi.control.domain.MainventilatorOne;
import com.ruoyi.control.domain.MainventilatorTwo;
import com.ruoyi.control.dto.AuxiliaryFanHistoryDto;
import com.ruoyi.control.dto.GasDrainagePumpRoomHistoryDto;
import com.ruoyi.control.mapper.GasDrainagePumpRoomMapper;
import com.ruoyi.control.mapper.MainventilatorOneMapper;
import com.ruoyi.control.mapper.MainventilatorTwoMapper;
import com.ruoyi.kepServer.aircompressor.mapper.AircompressorMapper;
import com.ruoyi.kepServer.centralpumphouse.domain.Centralpumphouse;
import com.ruoyi.kepServer.centralpumphouse.mapper.CentralpumphouseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.control.mapper.GasDrainagePumpRoomHistoryMapper;
import com.ruoyi.control.domain.GasDrainagePumpRoomHistory;
import com.ruoyi.control.service.IGasDrainagePumpRoomHistoryService;

/**
 * 瓦斯抽放泵房Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-08
 */
@Service
public class GasDrainagePumpRoomHistoryServiceImpl extends ServiceImpl<GasDrainagePumpRoomHistoryMapper, GasDrainagePumpRoomHistory> implements IGasDrainagePumpRoomHistoryService //修改
{
    @Autowired
    private GasDrainagePumpRoomHistoryMapper gasDrainagePumpRoomHistoryMapper;
    @Autowired
    private MainventilatorTwoMapper mainventilatorTwoMapper;
    @Autowired
    private MainventilatorOneMapper mainventilatorOneMapper;
    @Autowired
    private GasDrainagePumpRoomMapper gasDrainagePumpRoomMapper;
    @Autowired
    private AircompressorMapper aircompressorMapper;
    @Autowired
    private CentralpumphouseMapper centralpumphouseMapper;

    @Autowired
    private RedisCache redisCache;
    /**
     * 查询瓦斯抽放泵房
     *
     * @param id 瓦斯抽放泵房主键
     * @return 瓦斯抽放泵房
     */
    @Override
    public GasDrainagePumpRoomHistory selectGasDrainagePumpRoomHistoryById(String id)
    {
        return gasDrainagePumpRoomHistoryMapper.selectGasDrainagePumpRoomHistoryById(id);
    }

    /**
     * 查询瓦斯抽放泵房列表
     *
     * @param gasDrainagePumpRoomHistory 瓦斯抽放泵房
     * @return 瓦斯抽放泵房
     */
    @Override
    public List<GasDrainagePumpRoomHistory> selectGasDrainagePumpRoomHistoryList(GasDrainagePumpRoomHistory gasDrainagePumpRoomHistory)
    {
        return gasDrainagePumpRoomHistoryMapper.selectGasDrainagePumpRoomHistoryList(gasDrainagePumpRoomHistory);
    }

    /**
     * 新增瓦斯抽放泵房
     *
     * @param gasDrainagePumpRoomHistory 瓦斯抽放泵房
     * @return 结果
     */
    @Override
    public int insertGasDrainagePumpRoomHistory(GasDrainagePumpRoomHistory gasDrainagePumpRoomHistory)
    {
        return gasDrainagePumpRoomHistoryMapper.insertGasDrainagePumpRoomHistory(gasDrainagePumpRoomHistory);
    }

    /**
     * 修改瓦斯抽放泵房
     *
     * @param gasDrainagePumpRoomHistory 瓦斯抽放泵房
     * @return 结果
     */
    @Override
    public int updateGasDrainagePumpRoomHistory(GasDrainagePumpRoomHistory gasDrainagePumpRoomHistory)
    {
        gasDrainagePumpRoomHistory.setUpdateTime(DateUtils.getNowDate());
        return gasDrainagePumpRoomHistoryMapper.updateGasDrainagePumpRoomHistory(gasDrainagePumpRoomHistory);
    }

    /**
     * 批量删除瓦斯抽放泵房
     *
     * @param ids 需要删除的瓦斯抽放泵房主键
     * @return 结果
     */
    @Override
    public int deleteGasDrainagePumpRoomHistoryByIds(String[] ids)
    {
        return gasDrainagePumpRoomHistoryMapper.deleteGasDrainagePumpRoomHistoryByIds(ids);
    }

    /**
     * 删除瓦斯抽放泵房信息
     *
     * @param id 瓦斯抽放泵房主键
     * @return 结果
     */
    @Override
    public int deleteGasDrainagePumpRoomHistoryById(String id)
    {
        return gasDrainagePumpRoomHistoryMapper.deleteGasDrainagePumpRoomHistoryById(id);
    }

    @Override
    public Integer[] selectByTagName(String tagName) {
        Integer[] history= new Integer[60*24];
        List<GasDrainagePumpRoomHistory> historyList=gasDrainagePumpRoomHistoryMapper.selectByTagName(tagName);
        for (int i=0; i<60*24;i++){
            if (i>=historyList.size()){
                history[i]= 0;
            }else {
                double doubleValue = Double.parseDouble(historyList.get(i).getValue());
                int intValue = (int) doubleValue; // 如果需要四舍五入，可以使用 Math.round(doubleValue)
                history[i] = intValue;
            }
        }
        return history;
    }

    @Override
    public String[] getRealNum() {
        String[]  arr=new String[12];

        arr[0]= String.valueOf(gasDrainagePumpRoomMapper.selectGasDrainagePumpRoomByTagName("1#瓦斯泵效率").getValue());
        arr[1]= String.valueOf(gasDrainagePumpRoomMapper.selectGasDrainagePumpRoomByTagName("2#瓦斯泵效率").getValue());
        arr[2]= String.valueOf(gasDrainagePumpRoomMapper.selectGasDrainagePumpRoomByTagName("3#瓦斯泵效率").getValue());
        arr[3]= String.valueOf(gasDrainagePumpRoomMapper.selectGasDrainagePumpRoomByTagName("4#瓦斯泵效率").getValue());
        arr[4]= String.valueOf(mainventilatorOneMapper.selectMainventilatorOneByName("1#1级效率").getValue());
        arr[5]= String.valueOf(mainventilatorOneMapper.selectMainventilatorOneByName("2#2级效率").getValue());
        arr[6]= String.valueOf(centralpumphouseMapper.selectCentralpumphouseByTagName("1#水泵效率").getValue());
        arr[7]= String.valueOf(centralpumphouseMapper.selectCentralpumphouseByTagName("2#水泵效率").getValue());
        arr[8]= String.valueOf(centralpumphouseMapper.selectCentralpumphouseByTagName("3#水泵效率").getValue());
        arr[9]= String.valueOf(aircompressorMapper.selectAircompressorByTagName("1#压风机效率").getValue());
        arr[10]= String.valueOf(aircompressorMapper.selectAircompressorByTagName("2#压风机效率").getValue());
        arr[11]= String.valueOf(aircompressorMapper.selectAircompressorByTagName("3#压风机效率").getValue());
        return arr;
    }


    /**
     * 瓦斯7天故障
     * @return
     */
    public List<GasDrainagePumpRoomHistoryDto> getGasTotalFault() {

        final String key = "wsGzKey";
        List<GasDrainagePumpRoomHistoryDto> cachedValueList = redisCache.getCacheObject(key);

        List<GasDrainagePumpRoomHistoryDto> list = gasDrainagePumpRoomHistoryMapper.getGasTotalFault();


        if (list == null || list.isEmpty()) {
            //list  为空
            List<GasDrainagePumpRoomHistoryDto> defaultList = new ArrayList<>();
            for (int i = 0; i < 7; i++) {
                GasDrainagePumpRoomHistoryDto abnormalCountDto = new GasDrainagePumpRoomHistoryDto();
                abnormalCountDto.setCount("0");
                defaultList.add(abnormalCountDto);
            }
            List<String> collect = defaultList.stream().map(objInList -> {
                String abnormalCount = objInList.getCount();
                return abnormalCount;
            }).collect(Collectors.toList());
            redisCache.setCacheObject(key, collect);
        } else {
            List<GasDrainagePumpRoomHistoryDto> resultList = new ArrayList<>(list);
            while (resultList.size() < 7) {
                GasDrainagePumpRoomHistoryDto abnormalCountDto = new GasDrainagePumpRoomHistoryDto();
                abnormalCountDto.setCount("0");
                if (abnormalCountDto.getUpdateDate() != null) {
                    abnormalCountDto.setUpdateDate(abnormalCountDto.getUpdateDate());
                }
                resultList.add(abnormalCountDto);
            }

            redisCache.setCacheObject(key, resultList);
        }

        return cachedValueList;
    }

    public List<GasDrainagePumpRoomHistoryDto> getGasPowerValueList(String str) {
        String[] keys = {"wsGhKey1", "wsGhKey2", "wsGhKey3", "wsGhKey4"};
        String[] prefixes = {"1#", "2#", "3#", "4#"};

        int index = Integer.parseInt(str) - 1;
        String key = keys[index];
        String dy = prefixes[index] + "电压";
        String dl = prefixes[index] + "电流";

        List<GasDrainagePumpRoomHistoryDto> cachedValueList = redisCache.getCacheObject(key);
        if (cachedValueList != null) {
            return cachedValueList;
        }

        List<GasDrainagePumpRoomHistoryDto> list = gasDrainagePumpRoomHistoryMapper.getGasPowerValueList(dy, dl);
        List<GasDrainagePumpRoomHistoryDto> processedList = list.stream().map(objInList -> {
            Double avgPower = objInList.getAvgPower();
            Double consumptionValue = avgPower * 60 * 60;
            objInList.setConsumptionValue(Double.valueOf(String.format("%.2f", consumptionValue)));
            return objInList;
        }).collect(Collectors.toList());

        redisCache.setCacheObject(key, processedList);
        return processedList;
    }
}
